package com.xiaotu.spider.baidu.pipeline;

import com.xiaotu.common.model.StarBaikeWorksModel;
import com.xiaotu.common.model.StarBaikeWorksRecordModel;
import com.xiaotu.common.model.StarModel;
import com.xiaotu.common.model.StarStringModel;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.APipeline;
import com.xiaotu.storage.converter.AConverter;
import com.xiaotu.storage.converter.ConverterFactory;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.io.File;
import java.util.List;


@Component("StarBaikeWorksInfoPipeline")
public class StarBaikeWorksInfoPipeline extends APipeline
{
    private static final String[] STAR_BAIKE_WORKS_INFO = new String[]{"starId", "starName","tvName", "actRole","director","mainActor","link"};

    private static final String[] CONVERTER_STAR_BAIKE_WORKS = new String[]{
            AConverter.STRING_CONVERT, AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT,AConverter.STRINGARRAY_CONVERT,AConverter.STRINGARRAY_CONVERT,AConverter.STRING_CONVERT};

    private static final boolean[] THROW_STAR_BAIKE_WORKS = new boolean[]{true, true, true, true, true,true,true};

    private static final String[] STAR_BAIKE_WORKS_RECORD_INFO = new String[]{"starId", "starName","baikeId","status"};

    private static final String[] CONVERTER_STAR_BAIKE_WORKS_RECORD = new String[]{AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,AConverter.INTEGER_CONVERT};

    private static final boolean[] THROW_STAR_BAIKE_WORKS_RECORD = new boolean[]{true,true,true,true};


    @Override
    protected void doProcess(ResultItems resultItems, Task task)
    {

        List<StarBaikeWorksModel> starModelList = resultItems.get(StarBaikeWorksModel.class.getName());
        if (null !=starModelList && starModelList.size() > 0){
            for (StarBaikeWorksModel starBaikeWorksModel : starModelList) {
                String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.STAR_BAIKE_WORKS_INFO_YYYYMMDD);
                cacheHandler.hSet(key, starBaikeWorksModel.getStarId()+"_"+UUIDUtils.getId(), starBaikeWorksModel);
            }
        }

        StarBaikeWorksRecordModel recordModel = resultItems.get(StarBaikeWorksRecordModel.class.getName());
        if (recordModel != null){
            String key2 = DataRedisKeyUtils.getKey(Constants.DataRedisKey.STAR_BAIKE_WORKS_RECORD_INFO_YYYYMMDD);
            cacheHandler.hSet(key2, recordModel.getStarId()+"_"+UUIDUtils.getId(), recordModel);
        }


        String filePath = resultItems.get("filePath");
        List<StarBaikeWorksModel> starModelCsvList = resultItems.get(StarBaikeWorksModel.class.getName());
        if (null !=starModelCsvList && starModelCsvList.size() > 0){
            for (StarBaikeWorksModel starModel : starModelCsvList) {
                FileUtil.writeFile(filePath + File.separator + "starBaikeWorksInfo.csv", StringUtil.list2CSVLine
                        (ConverterFactory.convert(JSONObject.fromObject(starModel), STAR_BAIKE_WORKS_INFO,
                                CONVERTER_STAR_BAIKE_WORKS, THROW_STAR_BAIKE_WORKS), SepratorUtil.SEP_PIPE) +
                        SepratorUtil.LINE_SEPRATOR);
            }
        }

        StarBaikeWorksRecordModel starRecordModel = resultItems.get(StarBaikeWorksRecordModel.class.getName());
        if (starRecordModel != null){
            FileUtil.writeFile(filePath + File.separator + "starBaikeWorksRecordInfo.csv", StringUtil.list2CSVLine
                    (ConverterFactory.convert(JSONObject.fromObject(starRecordModel), STAR_BAIKE_WORKS_RECORD_INFO,
                            CONVERTER_STAR_BAIKE_WORKS_RECORD, THROW_STAR_BAIKE_WORKS_RECORD), SepratorUtil.SEP_PIPE) +
                    SepratorUtil.LINE_SEPRATOR);
        }

    }
}
